import Router from "src/router";

const state = {
  visitedViews: [],
  actionTagPath: '',
}

const mutations = {
  ADD_VISITED_VIEW: (state, view) => {

    if (state.visitedViews.some(v => v.path === view.path)) return

    state.visitedViews.push({
      path: view.path,
      title: view.meta.title,
      query: view.query,
      fullPath: view.fullPath,
      name: view.name,
    })
  },
  REMOVE_VISITED_VIEW: (state, view) => {
    let index = state.visitedViews.findIndex(v => v.path === view.path)
    let delVisitedView = state.visitedViews[index]
    state.visitedViews.splice(index, 1)

    let length = state.visitedViews.length;

    if (length === 0) {
      // 没有了，跳转到首页
      if (view.path === '/home') {
        state.visitedViews.push(delVisitedView)
      } else {
        Router.push({path: '/home'})
      }
      return
    }

    if (view.path === state.actionTagPath) {
      // 如果关掉的是当前页面
      if (length === index) {
        // 如果是最后一个
        let visitedView = state.visitedViews[index - 1]
        Router.push({ path: visitedView.path, query: visitedView.query, fullPath: visitedView.fullPath })
      } else {
        let visitedView = state.visitedViews[index]
        Router.push({ path: visitedView.path, query: visitedView.query, fullPath: visitedView.fullPath })
      }
    }
  },
  UPDATE_ACTION_TAG_PATH: (state, path) => {
    state.actionTagPath = path
  },
  REMOVE_ALL_VISITED_VIEW: (state) => {
    let homeIndex = state.visitedViews.findIndex(v => v.path === '/home')

    if (homeIndex === -1 || state.actionTagPath !== '/home') {
      state.visitedViews.splice(0, state.visitedViews.length)
      Router.push({path: '/home'})
      return
    }

    let homeVisitedView = state.visitedViews[homeIndex]
    state.visitedViews.splice(0, state.visitedViews.length)
    state.visitedViews.push(homeVisitedView)
  },
  REMOVE_OTHER_VISITED_VIEW: (state, type) => {
    let actionIndex = state.visitedViews.findIndex(v => v.path === state.actionTagPath)

    if (actionIndex === -1) {
      state.visitedViews.splice(0, state.visitedViews.length)
      Router.push({path: '/home'})
      return
    }

    if (type === 0) {
      let actionVisitedView = state.visitedViews[actionIndex]
      state.visitedViews.splice(0, state.visitedViews.length)
      state.visitedViews.push(actionVisitedView)
      return
    }

    if (type === -1) {
      state.visitedViews.splice(0, actionIndex)
      return
    }

    if (type === 1) {
      state.visitedViews.splice(actionIndex + 1, state.visitedViews.length)
      return
    }

  },
  REMOVE_OTHER_VISITED_VIEW_BY_BTN: (state, data) => {
    let view = data.view
    let type = data.type
    let actionIndex = state.visitedViews.findIndex(v => v.path === state.actionTagPath)
    let viewIndex = state.visitedViews.findIndex(v => v.path === view.path)

    if (actionIndex === -1) {
      state.visitedViews.splice(0, state.visitedViews.length)
      Router.push({path: '/home'})
      return
    }

    if (type === 0) {
      if (viewIndex === actionIndex) {
        let viewVisitedView = state.visitedViews[viewIndex]
        state.visitedViews.splice(0, state.visitedViews.length)
        state.visitedViews.push(viewVisitedView)
      } else {
        let visitedView = state.visitedViews[viewIndex]
        state.visitedViews.splice(0, state.visitedViews.length)
        Router.push({ path: visitedView.path, query: visitedView.query, fullPath: visitedView.fullPath })
      }
      return
    }

    if (type === -1) {
      let visitedView = state.visitedViews[viewIndex]
      state.visitedViews.splice(0, viewIndex)
      if (viewIndex > actionIndex) {
        Router.push({ path: visitedView.path, query: visitedView.query, fullPath: visitedView.fullPath })
      }
      return
    }

    if (type === 1) {
      let visitedView = state.visitedViews[viewIndex]
      state.visitedViews.splice(viewIndex + 1, state.visitedViews.length)
      if (viewIndex < actionIndex)  {
        Router.push({ path: visitedView.path, query: visitedView.query, fullPath: visitedView.fullPath })
      }
    }

  }
}

const actions = {
  addView({dispatch}, view) {
    dispatch('addVisitedView', view)
  },
  addVisitedView({commit}, view) {
    commit('ADD_VISITED_VIEW', view)
    commit('UPDATE_ACTION_TAG_PATH', view.path)
  },
  removeView({dispatch}, view) {
    dispatch('removeVisitedView', view)
  },
  removeVisitedView({commit}, view) {
    commit('REMOVE_VISITED_VIEW', view)
  },
  removeAllView({dispatch}) {
    dispatch('removeAllVisitedView')
  },
  removeAllVisitedView({commit}) {
    commit('REMOVE_ALL_VISITED_VIEW')
    commit('UPDATE_ACTION_TAG_PATH', '/home')
  },
  removeOtherView({dispatch}, type) {
    dispatch('removeOtherVisitedView', type)
  },
  removeOtherVisitedView({commit}, type) {
    commit('REMOVE_OTHER_VISITED_VIEW', type)
  },
  removeOtherViewByBtn({dispatch}, data) {
    dispatch('removeOtherVisitedViewByBtn', data)
  },
  removeOtherVisitedViewByBtn({commit}, data) {
    commit('REMOVE_OTHER_VISITED_VIEW_BY_BTN', data)
  },
}

export default {
  namespaced: true,
  mutations,
  state,
  actions,
}
